package com.pro.service.imple;

import com.pro.dao.AersonnelStatisticsMapper;
import com.pro.service.AersonnelStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;

/**
 * @author ：刘弘禹
 * @description：TODO
 * @date ：2021/12/6 18:29
 */
@Service
public class AersonnelStatisticsServiceImple implements AersonnelStatisticsService {
    @Autowired
    private AersonnelStatisticsMapper aersonnelStatisticsMapper;

    @Override
    public Integer getFrontEndNumber(String tableName) {
        Integer frontEndNumber = aersonnelStatisticsMapper.getFrontEndNumber(tableName);
        return frontEndNumber;
    }

    @Override
    public List getMonthlyNumber(String tabName,String dateName) {
        List<Map<String,Object>> monthlyNumber = aersonnelStatisticsMapper.getMonthlyNumber(tabName,dateName);
        Object[] obj = new Object[12];
        for (Map<String, Object> stringIntegerMap : monthlyNumber) {
            int currentMonth = Integer.parseInt(stringIntegerMap.get("currentMonth").toString());
            obj[currentMonth-1] = stringIntegerMap.get("num").toString();
        }
        for(int i = 0; i<obj.length; i++){
            if(obj[i] == null){
                obj[i] = 0;
            }
        }
        return Arrays.asList(obj);
    }

    @Override
    public List<Map<String, Object>> getGetKeyword() {
        List<Map<String, Object>> getKeyword = aersonnelStatisticsMapper.getGetKeyword("js_search_keywords","search_keywords","countNum");
        /*for (int i = 0; i < getKeyword.size(); i++) {
            for (int j = i+1; j < getKeyword.size(); j++) {
                if(getKeyword.get(j).get("search") != null && getKeyword.get(i).get("search") != null ){
                    if(getKeyword.get(i).get("search").equals(getKeyword.get(j).get("search"))) {
                        getKeyword.get(i).put("hits",Integer.parseInt(getKeyword.get(i).get("hits").toString()) + Integer.parseInt(getKeyword.get(j).get("hits").toString()));
                        getKeyword.remove(j);
                    }
                }
            }
        }
        Collections.sort(getKeyword, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                if(Integer.parseInt(o1.get("hits").toString())>Integer.parseInt(o2.get("hits").toString())){
                    return -1;
                }else if(Integer.parseInt(o1.get("hits").toString()) < Integer.parseInt(o2.get("hits").toString())){
                    return 1;
                }else{
                    return 0;
                }
            }
        });*/
        return getKeyword;
    }


}
